package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.HouseMapper;
import com.bw.mapper.RegionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.UploadPicUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {


    @Autowired
    RegionMapper regionMapper;
    @Autowired
    HouseMapper houseMapper;

    @RequestMapping("/list.do")
    public String list(HouseQuery query, Integer pageNum, HttpServletRequest request, Model model) {
        //1. 展示昵称和头像
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("userFromRequest", userFromRequest);

        //2. 渲染下拉框
        List<Dict> provinceList = regionMapper.selectProvince();
        model.addAttribute("provinceList", provinceList);

        //3. 分页查询
        PageHelper.startPage(pageNum == null ? 1:pageNum,3);
        List<House> list = houseMapper.selectByParam(query);
        PageInfo<House> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);

        return "list";

    }

    @RequestMapping("/selectCity.do")
    @ResponseBody
    public List<Dict> selectCity(Integer provinceId){
        return regionMapper.selectCity(provinceId);
    }
    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //2. 渲染下拉框
        List<Dict> provinceList = regionMapper.selectProvince();
        model.addAttribute("provinceList", provinceList);
        return "add";
    }

    @RequestMapping("/add.do")
    public String add(@Valid HouseAdd houseAdd, BindingResult result,Model model,HttpServletRequest request){

        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //2. 渲染下拉框
            List<Dict> provinceList = regionMapper.selectProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";
        }
        MultipartFile picFile = houseAdd.getPicFile();

        String url = UploadPicUtils.upload(picFile, request);
        houseAdd.setPicUrl(url);
        houseMapper.add(houseAdd);
        // 添加成功 ，发送短信 在什么时间添加了楼盘信息

        boolean b = ZzyUtils.sendSms("17611221735", System.currentTimeMillis() + "添加了楼盘", 5);

        if (b){
            //短信发送成功，修改楼盘信息短信发送的状态
            houseMapper.updateSms(houseAdd.getId());
        }

        return "redirect:list.do";
    }

}
